package com.onpositive.funkytable.test;

import java.io.FileNotFoundException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.Random;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import com.onpositive.data.IReadableObjectCollection;
import com.onpositive.data.serializer.readers.ObjectCollectionReader;
import com.onpositive.data.serializer.readers.RandomAccessFileAccess;
import com.onpositive.funkytable.DefaultTableBuilder;
import com.onpositive.funkytable.IFunkyComposite;
import com.onpositive.funkytable.Point;
import com.onpositive.funkytable.swt.ISWTControl;
import com.onpositive.funkytable.swt.SWTControlFactory;

public class Main {

	private static IFunkyComposite buildUI;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ObjectCollectionReader t = new ObjectCollectionReader();
		IReadableObjectCollection[] collection=null;
		try {
			collection = t
					.getCollection(new RandomAccessFileAccess(
							new RandomAccessFile(
									"C:\\work\\wiki new\\com.onpositive.funkytable.test\\src\\com\\onpositive\\funkytable\\test\\serialized.jpg","r")));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		final IReadableObjectCollection<Object> iReadableObjectCollection = collection[0];
		
		Display d =new Display();
		final Shell m=new Shell(d,SWT.SHELL_TRIM);
		m.setSize(600, 800);
		final ScrolledComposite mc=new ScrolledComposite(m, SWT.NONE);
		final Canvas uiI=new Canvas(m, SWT.DOUBLE_BUFFERED);
		Random r=new Random(2);
		int nextInt = r.nextInt(collection[0].size());
		ArrayList<SpecificationPart> createSpecParts = SpecPartsBuilder.createSpecParts(collection[0],nextInt);
		System.out.println(createSpecParts);
		mc.setContent(uiI);
		mc.setExpandHorizontal(true);
		mc.setExpandVertical(true);
		m.open();
		final Specification spec=new Specification(createSpecParts.toArray(new SpecificationPart[createSpecParts.size()]));
		final DefaultTableBuilder defaultTableBuilder = new DefaultTableBuilder();
		buildUI = defaultTableBuilder.buildUI(new SWTControlFactory(), 600, 800, spec);
		m.addDisposeListener(new DisposeListener() {
		
			
			@Override
			public void widgetDisposed(DisposeEvent arg0) {
				System.exit(0);
			}
		});
		uiI.addPaintListener(new PaintListener() {
			
			@Override
			public void paintControl(PaintEvent arg0) {
				ISWTControl c=(ISWTControl) buildUI;
				c.draw(arg0.gc, 0, 0, m.getSize().x, m.getSize().y);
			}
		});
		m.addControlListener(new ControlListener() {
			
			@Override
			public void controlResized(ControlEvent arg0) {
				int w = m.getSize().x;
				int h = m.getSize().y;
				buildUI = defaultTableBuilder.buildUI(new SWTControlFactory(), w, h, spec);
				ISWTControl c=(ISWTControl) buildUI;
				c.layout(m.getSize().x);
				
				Point measure = buildUI.measure(w, h);
				
				mc.setMinHeight(measure.y);
				mc.setMinWidth(w);
				uiI.setBounds(0, 0,w, measure.y );
				mc.layout(true);
			}
			
			@Override
			public void controlMoved(ControlEvent arg0) {
				
			}
		});
		while (true){
			d.readAndDispatch();
			d.sleep();
		}
	}

}
